home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-05 / src_1218.zip / HS.C < prev    next >
C/C++ Source or Header  |  1991-02-11  |  19KB  |  749 lines

  1. /* Interface driver for the DRSI PCPA or the Eagle 8530 boards for the IBM PC
  2.  * connected to a WA4DSY 56kbps modem. Uses polling-loop transfers with
  3.  * interrupts disabled for maximum speed.
  4.  *
  5.  * This driver is a bit of a kludge. A DMA-driven card and driver (e.g.,
  6.  * the PI) is much better, but this is better than nothing if all you have
  7.  * is a "dumb" 8530 card.
  8.  *
  9.  * Copyright 1991 Phil Karn, KA9Q
  10.  */
  11. #include <stdio.h>
  12. #include <dos.h>
  13. #include "global.h"
  14. #include "mbuf.h"
  15. #include "iface.h"
  16. #include "pktdrvr.h"
  17. #include "netuser.h"
  18. #include "hs.h"
  19. #include "8530.h"
  20. #include "ax25.h"
  21. #include "trace.h"
  22. #include "pc.h"
  23. #include "proc.h"
  24. #include "devparam.h"
  25.  
  26. static void flushrx __ARGS((int16 data));
  27. static void hdlcparam __ARGS((struct hdlc *hp));
  28. static void hexint __ARGS((struct hdlc *hp));
  29. static void hrxint __ARGS((struct hdlc *hp));
  30. static int hs_stop __ARGS((struct iface *iface));
  31. static int hs_raw __ARGS((struct iface *iface,struct mbuf *bp));
  32. static void hs_tx __ARGS((int unused,void *hp1,void *a));
  33. static int32 hs_ctl __ARGS((struct iface *,int cmd,int set,int32 val));
  34. static void hstxoff __ARGS((struct hdlc *hp));
  35. static void hstxon __ARGS((struct hdlc *hp));
  36. static void htxint __ARGS((struct hdlc *hp));
  37. static void init_delay __ARGS((void));
  38. static void msdelay __ARGS((void));
  39.  
  40. static struct hs Hs[NHS];
  41. static INTERRUPT (*Hshandle[])() = { hs0vec };
  42. static struct hdlc Hdlc[2*NHS];
  43. static int16 Nhs;
  44.  
  45. /* Master interrupt handler for the PC-100 card. All interrupts come
  46.  * here first, then are switched out to the appropriate routine.
  47.  */
  48. void
  49. hsint(dev)
  50. int dev;
  51. {
  52.     register char iv;
  53.     int16 hsbase;
  54.     register struct hdlc *hp;
  55.     
  56.     Hs[dev].ints++;
  57.     hsbase = Hs[dev].addr;
  58.  
  59. #ifdef    foo
  60.     outportb(hsbase+4,0x8+0x10);    /* HIT EAGLE INTACK */
  61.     (void)inportb(hsbase+CHANA+CTL,R0);
  62.     outportb(hsbase+4,0x8);        /***/
  63. #endif
  64.  
  65.     /* Read interrupt status from channel A */
  66.     while((iv = read_scc(hsbase+CHANA+CTL,R3)) != 0){
  67.         if(iv & CHARxIP){
  68.             /* Channel A Rcv Interrupt Pending */
  69.             hp = &Hdlc[2*dev];
  70.             hrxint(hp);
  71.         } else if(iv & CHATxIP){
  72.             /* Channel A Transmit Int Pending */
  73.             hp = &Hdlc[2*dev];
  74.             htxint(hp);
  75.         } else if(iv & CHAEXT){
  76.             /* Channel A External Status Int */
  77.             hp = &Hdlc[2*dev];
  78.             hexint(hp);
  79.         } else if(iv & CHBRxIP){
  80.             /* Channel B Rcv Interrupt Pending */
  81.             hp = &Hdlc[(2*dev)+1];
  82.             hrxint(hp);
  83.         } else if(iv & CHBTxIP){
  84.             /* Channel B Transmit Int Pending */
  85.             hp = &Hdlc[(2*dev)+1];
  86.             htxint(hp);
  87.         } else if(iv & CHBEXT){
  88.             /* Channel B External Status Int */
  89.             hp = &Hdlc[(2*dev)+1];
  90.             hexint(hp);
  91.         }
  92.         /* Reset interrupt pending state */
  93.         write_scc(hp->ctl,R0,RES_H_IUS);
  94.         outportb(hsbase+CHANA+CTL,0);    /* Restore pointer to 0 */
  95.         outportb(hsbase+CHANB+CTL,0);    /* Restore pointer to 0 */
  96.     }
  97.     outportb(hsbase+CHANA+CTL,0);    /* Restore pointer to 0 */
  98.     outportb(hsbase+CHANB+CTL,0);    /* Restore pointer to 0 */
  99. }
  100. /* HDLC SIO External/Status interrupts
  101.  * The only one that can happen in this driver is a DCD change
  102.  */
  103. static void
  104. hexint(hp)
  105. register struct hdlc *hp;
  106. {
  107.     struct mbuf *rcvbuf;
  108.     struct phdr phdr;
  109.     char *cp;
  110.     int cnt,data;
  111.     register int ctl;
  112.  
  113.     ctl = hp->ctl;
  114.     data = hp->data;
  115.     hp->exints++;
  116.  
  117.     /* Allocate a receive buffer */
  118.     if((rcvbuf = alloc_mbuf(hp->bufsiz+sizeof(phdr))) == NULLBUF){
  119.         /* Alloc failed; refuse to proceed */
  120.         hp->nomem++;
  121.         write_scc(ctl,R3,ENT_HM|RxENABLE|RxCRC_ENAB|Rx8);
  122.         write_scc(ctl,R0,RES_EXT_INT);
  123.         return;
  124.     }
  125.     /* Allow space for phdr descriptor on front */
  126.     cp = rcvbuf->data + sizeof(phdr);
  127.     cnt = 0;
  128.  
  129.     /* Disable DCDIE bit so we can track changes in DCD */
  130.     write_scc(ctl,R15,0);
  131.  
  132.     write_scc(ctl,R3,ENT_HM|RxENABLE|RxCRC_ENAB|Rx8);
  133.     flushrx(data);
  134.     while((cnt = rx8530(ctl,data,cp,hp->bufsiz)) != -1){
  135.         if(cnt > 4){
  136.             /* Good frame */
  137.             hp->good++;
  138.             /* Toss crc */
  139.             rcvbuf->cnt = sizeof(phdr) + cnt - 1;
  140.             phdr.iface = hp->iface;
  141.             phdr.type = CL_AX25;
  142.             memcpy(rcvbuf->data,(char *)&phdr,sizeof(phdr));
  143.             enqueue(&Hopper,rcvbuf);
  144.             /* Replenish buffer */
  145.             rcvbuf = alloc_mbuf(hp->bufsiz + sizeof(phdr));
  146.         }
  147.         /* Start new buffer */
  148.         if(rcvbuf == NULLBUF)
  149.             break;    /* alloc failed */
  150.         cp = rcvbuf->data + sizeof(phdr);
  151.     }    
  152.     write_scc(ctl,R0,RES_EXT_INT);
  153.     write_scc(ctl,R15,DCDIE);    /* Re-enable DCD */
  154.     write_scc(ctl,R3,ENT_HM|RxENABLE|RxCRC_ENAB|Rx8);
  155.  
  156.     /* Get rid of fragmentary buffer */
  157.     free_p(rcvbuf);
  158. }
  159. static void
  160. flushrx(data)
  161. register int16 data;
  162. {
  163.     register int i = 5;
  164.     while(i-- != 0)
  165.         (void)inportb(data);
  166. }
  167. /* HDLC receiver interrupt handler.
  168.  * Not used in this driver
  169.  */
  170. static void
  171. hrxint(hp)
  172. register struct hdlc *hp;
  173. {
  174. }
  175. /* HDLC transmit interrupt service routine
  176.  * Not used in this driver
  177.  */
  178. static void
  179. htxint(hp)
  180. register struct hdlc *hp;
  181. {
  182. }
  183.  
  184. /* (re)Initialize HDLC controller parameters */
  185. static void
  186. hdlcparam(hp)
  187. register struct hdlc *hp;
  188. {
  189.     char i_state;
  190.     register int16 ctl;
  191.  
  192.     /* Initialize 8530 channel for SDLC operation */
  193.     ctl = hp->ctl;
  194.     i_state = dirps();
  195.  
  196. #ifdef    foo
  197.     switch(ctl & 2){
  198.     case CHANA:
  199.         write_scc(ctl,R9,CHRA);    /* Reset channel A */
  200.         break;
  201.     case CHANB:
  202.         write_scc(ctl,R9,CHRB);    /* Reset channel B */
  203.         break;
  204.     }
  205.     pause(1L);    /* Allow plenty of time for resetting */
  206. #endif
  207.  
  208.     /* Deselect interrupts for now */
  209.     write_scc(ctl,R1,0);
  210.     write_scc(ctl,R15,0);
  211.  
  212.     /* X1 clock, SDLC mode, Sync modes enable, parity disable */
  213.     write_scc(ctl,R4,X1CLK | SDLC | SYNC_ENAB);
  214.  
  215.     /* CRC preset 1, NRZ encoding, no active on poll, flag idle,
  216.      * flag on underrun, no loop mode, 8 bit sync
  217.      */
  218.     write_scc(ctl,R10,CRCPS|NRZ);
  219.  
  220.     /* 8530 gets both tx and rx clock from modem.
  221.      * By default, TRxC = transmit clock, RTxC = receive clock
  222.      * (swapped 11 Feb 1990 to use new DRSI wiring) UNLESS
  223.      * the 'r' parameter is specified
  224.      */
  225.     if(!hp->clkrev)
  226.         write_scc(ctl,R11,RCRTxCP | TCTRxCP);
  227.     else
  228.         write_scc(ctl,R11,RCTRxCP | TCRTxCP);
  229.  
  230.     /* Note: baud rate generator not used */
  231.  
  232.     /* Null out SDLC start address */
  233.     write_scc(ctl,R6,0);
  234.  
  235.     /* SDLC flag */
  236.     write_scc(ctl,R7,FLAG);
  237.  
  238.     /* DTR On, 8 bit TX chars, no break, TX enable, SDLC CRC,
  239.      * RTS off, TxCRC enable
  240.      */
  241.     write_scc(ctl,R5,DTR|Tx8|TxENAB|TxCRC_ENAB);
  242.  
  243.     /* 8 bit RX chars, auto enables off, no hunt mode, RxCRC enable,
  244.      * no address search, no inhibit sync chars, disable RX. Rx is
  245.      * started only by an actual DCD interrupt
  246.      */
  247.     write_scc(ctl,R3,RxENABLE|RxCRC_ENAB|Rx8);
  248.  
  249.     /* Dummy interrupt vector
  250.      * (This probably isn't necessary)
  251.      */
  252.     write_scc(ctl,R2,0);
  253.  
  254.     /* Enable only the external interrupts (modem interrupts) since
  255.      * polling is used for all actual tx/rx operations
  256.      */
  257.     write_scc(ctl,R1,EXT_INT_ENAB);
  258.  
  259.     /* Enable only DCD interrupts */
  260.     write_scc(ctl,R15,DCDIE);
  261.  
  262.     /* No reset, status low, master int enable, enable lower chain,
  263.      * no vector
  264.      */
  265.     write_scc(ctl,R9,MIE|NV);
  266.  
  267.     restore(i_state);
  268. }
  269. /* Attach a high speed iterface to the system
  270.  * argv[0]: hardware type, must be "hs"
  271.  * argv[1]: I/O address, e.g., "0x380"
  272.  * argv[2]: vector, e.g., "2"
  273.  * argv[3]: mode, must be "ax25"
  274.  * argv[4]: interface base label, e.g., "drsi0". Driver appends "a" and "b".
  275.  * argv[5]: receiver packet buffer size in bytes
  276.  * argv[6]: maximum transmission unit, bytes
  277.  * argv[7]: keyup delay, milliseconds
  278.  * argv[8]: persistence value, 0-255
  279.  * argv[9]: "r" to reverse sense of clock leads (optional)
  280.  */
  281. int
  282. hs_attach(argc,argv,p)
  283. int argc;
  284. char *argv[];
  285. void *p;
  286. {
  287.     register struct iface *if_hsa,*if_hsb;
  288.     struct hdlc *hp;
  289.     int dev;
  290.  
  291.     if(Nhs >= NHS){
  292.         tprintf("Too many hs controllers\n");
  293.         return -1;
  294.     }
  295.     if(if_lookup(argv[4]) != NULLIF){
  296.         tprintf("Interface %s already exists\n",argv[4]);
  297.         return -1;
  298.     }
  299.     dev = Nhs++;
  300.  
  301.     /* Initialize hardware-level control structure */
  302.     Hs[dev].addr = htoi(argv[1]);
  303.     Hs[dev].vec = htoi(argv[2]);
  304.  
  305.     /* Save original interrupt vector */
  306.     Hs[dev].save.vec = getirq(Hs[dev].vec);
  307.     /* Set new interrupt vector */
  308.     if(setirq(Hs[dev].vec,Hshandle[dev]) == -1){
  309.         tprintf("IRQ %u out of range\n",Hs[dev].vec);
  310.         Nhs--;
  311.         return -1;
  312.     }
  313.     /* Create interface structures and fill in details */
  314.     if_hsa = (struct iface *)callocw(1,sizeof(struct iface));
  315.     if_hsb = (struct iface *)callocw(1,sizeof(struct iface));
  316.  
  317.     if_hsa->addr = if_hsb->addr = Ip_addr;
  318.     if_hsa->name = mallocw(strlen(argv[4])+2);
  319.     strcpy(if_hsa->name,argv[4]);
  320.     strcat(if_hsa->name,"a");
  321.     if_hsb->name = mallocw(strlen(argv[4])+2);
  322.     strcpy(if_hsb->name,argv[4]);
  323.     strcat(if_hsb->name,"b");
  324.     if_hsb->mtu = if_hsa->mtu = atoi(argv[6]);
  325.     if_hsb->type = if_hsa->type = CL_AX25;
  326.     if_hsa->dev = 2*dev;
  327.     if_hsb->dev = 2*dev + 1;
  328.     if_hsb->stop = if_hsa->stop = hs_stop;
  329.     if_hsb->output = if_hsa->output = ax_output;
  330.     if_hsb->raw = if_hsa->raw = hs_raw;
  331.     if_hsa->ioctl = if_hsb->ioctl = hs_ctl;
  332.  
  333.     if(strcmp(argv[3],"ax25") == 0){
  334.         if(Mycall[0] == '\0'){
  335.             tprintf("set mycall first\n");
  336.             free((char *)if_hsa);
  337.             free((char *)if_hsb);
  338.             return -1;
  339.         }        
  340.         if_hsb->send = if_hsa->send = ax_send;
  341.         if(if_hsb->hwaddr == NULLCHAR)
  342.             if_hsb->hwaddr = mallocw(AXALEN);
  343.         memcpy(if_hsb->hwaddr,Mycall,AXALEN);
  344.         if(if_hsa->hwaddr == NULLCHAR)
  345.             if_hsa->hwaddr = mallocw(AXALEN);
  346.         memcpy(if_hsa->hwaddr,Mycall,AXALEN);
  347.     } else {
  348.         tprintf("Mode %s unknown for interface %s\n",
  349.             argv[3],argv[4]);
  350.         free((char *)if_hsa);
  351.         free((char *)if_hsb);
  352.         return -1;
  353.     }
  354.     if_hsa->next = if_hsb;
  355.     if_hsb->next = Ifaces;
  356.     Ifaces = if_hsa;
  357.  
  358.     write_scc(Hs[dev].addr+CHANA+CTL,R9,FHWRES);
  359.     hp = &Hdlc[2*dev+1];
  360.     hp->ctl = Hs[dev].addr + CHANB + CTL;
  361.     hp->data = Hs[dev].addr + CHANB + DATA;
  362.     hp->bufsiz = atoi(argv[5]);
  363.     if(argc > 7)
  364.         hp->txdelay = atol(argv[7]);
  365.     else
  366.         hp->txdelay = 15L;
  367.     if(argc > 8)
  368.         hp->p = atoi(argv[8]);
  369.     else
  370.         hp->p = 64;
  371.     if(argc > 9 && argv[9][0] == 'r')
  372.         hp->clkrev = 1;
  373.     else
  374.         hp->clkrev = 0;
  375.     hp->iface = if_hsb;
  376.     hdlcparam(hp);
  377.     if_hsa->txproc = newproc("hs_tx",1024,hs_tx,0,hp,NULL,0);
  378.  
  379.     hp = &Hdlc[2*dev];
  380.     hp->ctl = Hs[dev].addr + CHANA + CTL;
  381.     hp->data = Hs[dev].addr + CHANA + DATA;
  382.     hp->bufsiz = atoi(argv[5]);
  383.     hp->txdelay = Hdlc[2*dev+1].txdelay;
  384.     hp->p = Hdlc[2*dev+1].p;
  385.     if(argc > 9 && argv[9][0] == 'r')
  386.         hp->clkrev = 1;
  387.     else
  388.         hp->clkrev = 0;
  389.     hp->iface = if_hsa;
  390.     hdlcparam(hp);
  391.     if_hsb->txproc = newproc("hs_tx",1024,hs_tx,0,hp,NULL,0);
  392.  
  393.     outportb(Hs[dev].addr + 4,0x08);    /*EAGLE INT GATE */
  394.     /* Clear mask (enable interrupt) in 8259 interrupt controller */
  395.     maskon(Hs[dev].vec);
  396.  
  397.     /* Initialize timing delay loop */
  398.     init_delay();
  399.     return 0;
  400. }
  401. static int
  402. hs_stop(iface)
  403. struct iface *iface;
  404. {
  405.     int dev;
  406.  
  407.     dev = iface->dev;
  408.     if(dev & 1)
  409.         return -1;    /* Valid only for the first device */
  410.     dev >>= 1;    /* Convert back into hs number */
  411.  
  412.     /* Turn off interrupts */
  413.     maskoff(Hs[dev].vec);
  414.  
  415.     /* Restore original interrupt vector */
  416.     setirq(Hs[dev].vec,Hs[dev].save.vec);
  417.  
  418.     /* Force hardware reset */
  419.     write_scc(Hs[dev].addr + CHANA+CTL,R9,FHWRES);
  420.     return 0;
  421. }
  422. /* Send raw packet */
  423. static int
  424. hs_raw(iface,bp)
  425. struct iface *iface;
  426. struct mbuf *bp;
  427. {
  428.  
  429.     struct hdlc *hp;
  430.  
  431.     dump(iface,IF_TRACE_OUT,CL_AX25,bp);
  432.     iface->rawsndcnt++;
  433.     iface->lastsent = secclock();
  434.     hp = &Hdlc[iface->dev];
  435.     hp->txpkts++;
  436.     enqueue(&hp->txq,bp);    /* Put on queue for hs_tx process */
  437.     return 0;
  438. }
  439.  
  440. /* High speed transmit process */
  441. void
  442. hs_tx(unused,hp1,a)
  443. int unused;
  444. void *hp1;
  445. void *a;    /* Unused */
  446. {
  447.     struct mbuf *bp,*nbp;
  448.     register int16 cnt;
  449.     register char *cp;
  450.     int16 ctl,data;
  451.     int txon = 0;    /* Transmitter on/off state */
  452.     struct hdlc *hp;
  453.     int i;
  454.  
  455.     hp = (struct hdlc *)hp1;
  456.     ctl = hp->ctl;
  457.     data = hp->data;
  458.  
  459.     for(;;){
  460.         /* Wait for work */
  461.         while(hp->txq == NULLBUF){
  462.             if(txon){
  463.                 /* No more frames, shut down tx */
  464.                 hstxoff(hp);
  465.                 txon = 0;
  466.                 /* Hold off to give other guy a chance to
  467.                  * respond
  468.                  */
  469.                 hp->deftime = msclock() + hp->txdelay + 500;
  470.             }
  471.             pwait(&hp->txq);
  472.         }
  473.         bp = dequeue(&hp->txq);
  474.         cnt = len_p(bp);
  475.         /* If buffer isn't contiguous (which is almost always
  476.          * the case) copy it to a new buffer for speed
  477.          */
  478.         if(bp->next != NULLBUF){
  479.             if((nbp = copy_p(bp,cnt)) == NULLBUF){
  480.                 hp->nomem++;
  481.                 free_p(bp);
  482.                 continue;
  483.             }
  484.             free_p(bp);
  485.             bp = nbp;
  486.         }
  487.         cp = bp->data;
  488.         /* Turn transmitter on if necessary */
  489.         if(!txon){
  490.             hstxon(hp);
  491.             txon = 1;
  492.         } else {
  493.             /* Else wait another txd for receiver to get ready again */
  494.             for(i=hp->txdelay;i != 0;i--)
  495.                 msdelay();
  496.         }
  497.         /* Initialize transmitter CRC */
  498.         write_scc(ctl,R0,RES_Tx_CRC);
  499.         for(;;){
  500.             /* Wait for the transmitter to become ready */
  501.             while(!(inportb(ctl) & Tx_BUF_EMP))
  502.                 ;
  503.             if(cnt-- == 0)
  504.                 break;
  505.             outportb(data,*cp++); /* Send the character */
  506.         }
  507.         write_scc(ctl,R0,RES_EOM_L);    /* Allow CRC generation */
  508.  
  509.         /* End of frame. Wait for TxEOM to go high, indicating start of
  510.          * CRC transmission. Note that we don't reset the transmit
  511.          * interrupt pending flag as one ordinarily would, since we're
  512.          * not using tx interrupts.
  513.          */
  514.         while(!(inportb(ctl) & TxEOM))
  515.             ;
  516.  
  517.         free_p(bp);
  518.     }
  519. }
  520.  
  521. /* Turn on high speed transmitter. Does p-persistence, then sends a dummy
  522.  * frame to allow for keyup delay. Returns with transmitter on and interrupts
  523.  * disabled
  524.  */
  525. static void
  526. hstxon(hp)
  527. struct hdlc *hp;
  528. {
  529.     int16 ctl;
  530.     int i;
  531.     long ca;
  532.     int32 t;
  533.  
  534.     ctl = hp->ctl;
  535.  
  536.     /* Defer logic. Wait until deftime is in the past (so we
  537.      * defer to any overheard CTS messages) AND the p-persistence
  538.      * dice roll succeeds. The computation of ca allows for clock
  539.      * rollover (which happens every 49+ days).
  540.      */
  541.     for(;;){
  542.         t = msclock();
  543.         ca = hp->deftime - t;
  544.         if(ca > 0){
  545.             pause(ca);
  546.             continue;
  547.         }
  548.         hp->deftime = t;    /* Keep from getting too old */
  549.         if((rand() & 0xff) > uchar(hp->p)){
  550.             pause((long)MSPTICK);
  551.             continue;
  552.         }
  553.         break;
  554.     }
  555.     /* Prevent distractions. In particular, block off the DCD interrupt
  556.      * so we don't hear our own carrier and hang in the interrupt handler!
  557.      * Note that simply disabling CPU interrupts isn't enough since
  558.      * the call to pause will block and the kernel will re-enable
  559.      * them.
  560.      */
  561.     write_scc(ctl,R9,0);    /* Disable all SCC interrupts */
  562.     (void)dirps();        /* Return value is always 1 */
  563.  
  564.     /* Turn on carrier, enable transmitter */
  565.     write_scc(ctl,R5,TxCRC_ENAB | RTS | TxENAB | Tx8 | DTR);
  566.  
  567.     /* Delay for keyup interval */
  568.     for(i=hp->txdelay;i != 0;i--)
  569.         msdelay();
  570.  
  571. }
  572. /* Turn transmitter off at the end of a series of frames */
  573. static void
  574. hstxoff(hp)
  575. struct hdlc *hp;
  576. {
  577.     int cnt;
  578.     int16 ctl,data;
  579.  
  580.     ctl = hp->ctl;
  581.     data = hp->data;
  582.     /* To allow the SCC buffering to drain, we begin a dummy frame,
  583.      * then abort it
  584.      */
  585.     for(cnt=5;cnt != 0;cnt--){
  586.         while(!(inportb(ctl) & Tx_BUF_EMP))
  587.             ;
  588.         outportb(data,0);
  589.     }
  590.     write_scc(ctl,R0,SEND_ABORT);
  591.  
  592.     /* Turn off carrier and disable transmitter */
  593.     write_scc(ctl,R5,TxCRC_ENAB | Tx8 | DTR);
  594.     /* Re-Enable SCC interrupts */
  595.     write_scc(ctl,R9,MIE|NV);        
  596.     restore(1);    /* Turn interrupts back on */
  597. }
  598.  
  599. int
  600. dohs(argc,argv,p)
  601. int argc;
  602. char *argv[];
  603. void *p;
  604. {
  605.     register int i;
  606.     register struct hdlc *hp;
  607.  
  608.     for(i=0;i<2*Nhs;i++){
  609.         hp = &Hdlc[i];
  610.         if(tprintf("port %d: txpkts %lu ints %lu rxpkts %lu rxbytes %lu nomem %lu toobig %lu crcerr %lu aborts %lu overrun %lu\n",
  611.          i,hp->txpkts,hp->exints,hp->good,hp->rxbytes,
  612.          hp->nomem,hp->toobig,hp->crcerr,hp->aborts,
  613.          hp->overrun) == EOF)
  614.             break;
  615.     }
  616.     return 0;
  617. }
  618. static int32
  619. hs_ctl(iface,cmd,set,val)
  620. struct iface *iface;
  621. int cmd;
  622. int set;
  623. int32 val;
  624. {
  625.     register struct hdlc *hp;
  626.     int32 t,ca;
  627.  
  628.     hp = &Hdlc[iface->dev];
  629.     switch(cmd){
  630.     case PARAM_TXDELAY:    /* Tx keyup delay */
  631.         if(set)
  632.             hp->txdelay = val;
  633.         return hp->txdelay;
  634.     case PARAM_PERSIST:
  635.         if(set)
  636.             hp->p = val;
  637.         return uchar(hp->p);
  638.     case PARAM_MUTE:
  639.         /* Mute transmitter for specified # of ms */
  640.         if(set){
  641.             if(val == -1){
  642.                 /* Special case for duration of a CTS */
  643.                 val = hp->txdelay + 500;
  644.             }
  645.             hp->deftime = msclock() + val;
  646.         }
  647.         t = msclock();
  648.         ca = hp->deftime - t;
  649.         if(ca < 0){
  650.             hp->deftime = t;
  651.             ca = 0;
  652.         }
  653.         return ca;
  654.     }
  655.     return -1;
  656. }
  657. #ifdef    notdef        /* replaced with assembler in 8530.asm */
  658. /* Read data from the 8530 receiver.
  659.  * Returns when either a good frame is received, or when carrier drops.
  660.  * If a good frame is received, the length is returned; otherwise -1.
  661.  */
  662. int
  663. rx8530(ctl,data,buf,bufsize)
  664. int16 ctl,data;
  665. char *buf;
  666. int16 bufsize;
  667. {
  668.     int cnt = 0;
  669.     register char status;
  670.     char error;
  671.     register char *cp = buf;
  672.  
  673.     for(;;){
  674.         status = inportb(ctl);
  675.         if(!(status & DCD)){
  676.             cnt = -1;
  677.             break;
  678.         } else if(status & BRK_ABRT){
  679.             cp = buf;
  680.             cnt = 0;
  681.         } else if(status & Rx_CH_AV){
  682.             /* Receive character is ready, get it */
  683.             *cp++ = inportb(data);
  684.             if(++cnt > bufsize){
  685.                 /* Buffer overflow, start again */
  686.                 write_scc(ctl,R3,ENT_HM|RxENABLE|RxCRC_ENAB|Rx8);
  687.                 cp = buf;
  688.                 cnt = 0;
  689.             }
  690.         } else if((error = read_scc(ctl,R1)) & END_FR){
  691.             if(!(error & CRC_ERR))
  692.                 break;    /* Good frame! */
  693.             /* Bad frame, start again */
  694.             cp = buf;
  695.             cnt = 0;
  696.         }
  697.     }
  698.     return cnt;
  699. }
  700. #endif
  701.  
  702. static int32 Del_const;
  703.  
  704. /* Find the value of Del_const that will cause one execution of mloop()
  705.  * to take one millisecond
  706.  */
  707. static void
  708. init_delay()
  709. {
  710.     int32 start,delay;
  711.     register int i,j;
  712.     int success = 0;
  713.  
  714.     /* Start with small value to make things tolerable on slow machines */
  715.     Del_const = 10;
  716.     printf("Del_const = %lu\n",Del_const);
  717.     /* Limit the number of iterations in case we don't converge */
  718.     for(i=0;i<5;i++){
  719.         start = msclock();
  720.         for(j=0;j<1000;j++)
  721.             msdelay();
  722.         delay = msclock()-start;
  723.         printf("delay %lu\n",delay);
  724.         if(delay == 0){
  725.             /* Too fast for accurate measurement on coarse clk */    
  726.             Del_const *= 10;
  727.             printf("Del_const = %lu\n",Del_const);
  728.             continue;
  729.         }
  730.         Del_const = (Del_const * 1000)/delay;
  731.         printf("Del_const = %lu\n",Del_const);
  732.         if(delay > 950 && delay < 1050){
  733.             success = 1;
  734.             break;    /* Within 1 tick - Close enough */
  735.         }
  736.     }
  737.     if(!success)
  738.         tprintf("HS: Warning: auto delay set failed\n");
  739. }
  740. /* Delay for one millisecond (once calibrated by init_delay()) */
  741. static void
  742. msdelay()
  743. {
  744.     int32 i;
  745.  
  746.     for(i=Del_const;i !=0;i--)
  747.         ;
  748. }
  749.